package com.sz.util;

import org.json.JSONObject;

import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ReadWriteProperties {

    public static void main(String[] args) {
        String path = "D:/jdbc.properties";
        Map<String, String> params = new HashMap<String, String>();
        params.put("serverip", "jdbc:db2://127.0.0.1:60000/DB_TOLL".replaceAll(":", "\u003A"));
        write(path, params);
        read(path);
    }
    
    /**
     * 将json的值写入properties中
     * @param json
     */
    public static void writePro(String propertiesPath, JSONObject json){
        if(ToolStr.isEmpty(json)){
            System.out.println("JSONObject不可为空");
        }

        Map<String, String> params = new HashMap<String, String>();
        for(Object keyObj : json.keySet()){
            String key = keyObj + "";
            params.put(key, json.getString(key));
        }
        write(propertiesPath, params);
        System.out.println(propertiesPath + "修改成功");
    }
    
    public static Properties getProperties(String path){
        Properties pro = new Properties();
        FileInputStream in = null;
        try {
            in = new FileInputStream(path);
            pro.load(in);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                in = null;
            }
        }
        return pro;
    }
    
    public static Properties read(String path){
        Properties pro = getProperties(path);
        print(pro);
        return pro;
    }
    
    public static void print(Properties pro){
        if(pro == null){
            System.out.println("Properties 是空的");
            System.exit(0);
        }
        Enumeration<Object> keys = pro.keys();
        while(keys.hasMoreElements()){
            Object key = keys.nextElement();
            System.out.println(key + " = " + pro.get(key));
        }
    }
    
    public static void write(String path, Map<String, String> params){
        if(ToolStr.isEmpty(params)){
            System.out.println("请输入修改参数");
            System.exit(0);
        }
        Properties pro = getProperties(path);
        
        FileOutputStream oFile = null;
        try {
            oFile = new FileOutputStream(path);
            for(String key : params.keySet()){
                pro.setProperty(key, params.get(key));
            }
//          如果comments不为空，保存后的属性文件第一行会是#comments,表示注释信息；如果为空则没有注释信息。
            pro.store(oFile, "comments");
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(oFile != null){
                try {
                    oFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                oFile = null;
            }
        }
        fileAppender(path);
    }
    
    public static void fileAppender(String fileName) {
        BufferedReader reader = null;
        FileOutputStream mm = null;
        try {
            reader = new BufferedReader(new FileReader(fileName));
            String line = null;
            // 一行一行的读
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\r\n");
            }
            //写回去
            mm = new FileOutputStream(fileName);
            String content = sb.toString().replaceAll("\\\\:", ":");
            mm.write(content.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(mm != null){
                try {
                    mm.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mm = null;
            }
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                reader = null;
            }
        }
    }

}
